/**
 * 
 */
package com.yidu.user.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.model.ManagePayIn;
import com.yidu.model.ManagePayOut;
import com.yidu.model.ManageUser;
import com.yidu.model.Page;
import com.yidu.model.Plot;
import com.yidu.user.dao.ManagePayOutMapper;
import com.yidu.user.dao.ManageUserMapper;

@Service(value="payOutService")
public class ManagePayOutServiceImpl implements ManagePayOutService{
	@Resource
	private ManagePayOutMapper payOutMapper;
	@Resource
	private ManageUserMapper userMapper;

	@Override
	public int getAllCount(String userId) {
		
		return payOutMapper.getAllCount(userId);
	}
	/**
	 * 查找所有的支出信息
	 */
	@Override
	public List<ManagePayOut> getAllPayOut(Page page,ManagePayOut pay) {
		//定义页数和行数
		if(page.getPages()==0){
			page.setRows(10);
			page.setPages(1);
		}
		if(page.getTotal()==0){
			if(page.getUserId()==null || "".equals(page.getUserId())){
				page.setTotal(getAllCount(pay.getUsName()));
			}else{
				//根据用户名查找信息
				ManageUser us = userMapper.getIdByName(page.getUserId());
				pay.setUsName(us.getUserId());
				page.setTotal(getAllCount(pay.getUsName()));
			}
		}
		if(page.getUserId()==null || "".equals(page.getUserId())){
			
			page.setUserId(pay.getUsName());
			ManageUser us = userMapper.getIdByName(page.getUserId());
			page.setUserId(us.getUserId());
			List<ManagePayOut> list = payOutMapper.getAllPayOut(page);
			return list;
		}else{
			ManageUser us = userMapper.getIdByName(page.getUserId());
			page.setUserId(us.getUserId());
			List<ManagePayOut> list = payOutMapper.getAllPayOut(page);
			return list;
		}
	}

	@Override
	public int addPayOut(ManagePayOut payOut) {
		if(!"".equals(payOut.getPayoutId())){
			//查询出修改前的钱
			ManagePayOut pay = payOutMapper.showUpdate(payOut.getPayoutId());
			//查询出该用户信息
			ManageUser us = userMapper.getIdByName(payOut.getUsName());
			//修改钱数，
			us.setUserBalance(us.getUserBalance()+pay.getPayoutMoney()-payOut.getPayoutMoney());
			//修改用户信息
			userMapper.updateByPrimaryKeySelective(us);
			//修改之处信息
			int row = payOutMapper.updateByPayOutId(payOut);
			return row;
		}else{
			//增加
			//支出主键
			payOut.setPayoutId(UUID.randomUUID().toString().replaceAll("-", ""));
			//查询出该用户信息
			ManageUser us = userMapper.getIdByName(payOut.getUsName());
			//减少用户钱
			us.setUserBalance(us.getUserBalance()-payOut.getPayoutMoney());
			
			//修改用户信息
			userMapper.updateByPrimaryKeySelective(us);
			payOut.setUser(us);
			//设置当前时间
			Date date = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sf.format(date);
			payOut.setPayoutDate(time);
			//增加支出详情
			int row = payOutMapper.addPayOut(payOut);
			return row;
		}
		
	}

	@Override
	public int deleteByPayOutId(String payOutId,String usName) {
		//查询
		ManagePayOut pay = payOutMapper.showUpdate(payOutId);
		//查询出该用户信息
		ManageUser us = userMapper.getIdByName(usName);
		//修改钱数，
		us.setUserBalance(us.getUserBalance()+pay.getPayoutMoney());
		//修改用户信息
		userMapper.updateByPrimaryKeySelective(us);
		return payOutMapper.deleteByPayOutId(payOutId);
	}
	@Override
	public ManagePayOut showUpdate(String payoutId) {
		
		return payOutMapper.showUpdate(payoutId);
	}
	@Override
	public List<Plot> selectMoney(String id) {
		
		return payOutMapper.selectMoney(id);
	}
}
